package me.zhengjie.cron;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.ppService.ImitationTestService;
import me.zhengjie.ppService.PPInBoundService;
import me.zhengjie.ppService.PPTaskResultService;
import me.zhengjie.pojo.sqlServer.PPInboundMission;
import me.zhengjie.pojo.sqlServer.PPTaskResult;
import me.zhengjie.modbusTCP.EntranceTCP;
import me.zhengjie.wcs.PPWcsStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: xinglin
 * @CreateTime: 2024-08-16
 * @Description: 定时查询是否有入库任务
 * @Version: 1.0
 */


@Slf4j
@Component
@RequiredArgsConstructor
public class PPInBoundCron {

    private final PPWcsStatus ppWcsStatus;

    private final EntranceTCP entranceTCP;
    private final PPTaskResultService ppTaskResultService;
    private final PPInBoundService ppInBoundService;
    private final ImitationTestService imitationTestService;

    private static final Logger ppLog = LoggerFactory.getLogger("pp");


    //@Transactional
    @Scheduled( fixedDelay = 3000)
    public void StartInBoundTask() {
        if (!ppWcsStatus.allowSendTask()) {
            ppLog.error("当前码垛机不能接收新任务");
        } else {


            // 查找有没有正在进行的入库任务
            PPTaskResult ppTaskResult = ppTaskResultService.findNowppTaskResult(1);
            // 查找最新的入库载盘任务
            PPInboundMission newPPInboundMission = ppInBoundService.findNewInBoundMission();
            //try {
                if (ObjectUtil.isEmpty(ppTaskResult) && ObjectUtil.isNotEmpty(newPPInboundMission)) {
                    ppLog.info("查询到码垛机空闲，开始发送入库任务");
                    ppInBoundService.sendInBoundTask(newPPInboundMission);
                    ppLog.info("发送入库任务到wcs成功，任务wms_task_id：{}", newPPInboundMission.getMissionCode());
                }
            //} catch (Exception e) {
            //    ppLog.error("入库任务发送到wcs失败，失败原因：{}", e.toString());

          //  }
        }


    }





}
